package com.qf.co.controller;

import com.qf.co.pojo.User;
import com.qf.co.service.UserServiceImpl;
import com.qf.co.service.UserService;
import com.qf.co.utils.StrUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author hanjiejie
 * @version V1.0
 * @Project java2203
 * @Package com.qf.co.controller
 * @Description:
 * @Date 2022/5/27 13:35
 */
@WebServlet("/user")
public class Usercontroller extends  BaseServlet {
    //注册
    public String register(HttpServletRequest request, HttpServletResponse response){
        //获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //调用业务层
        UserService userService = new UserServiceImpl();
        Boolean user = userService.check(username);
        if (!user){
            request.setAttribute("msg","用户名不可以");
            return "redirect:/register.jsp";
        }

        int count = userService.createUser(username,password);
      if(count>0){
          return "redirect:/login.jsp";
      }else {
          return "redirect:/register.jsp";
      }

    }

    /**
     * 登录
     * @param request
     * @param response
     * @return
     */
    public String login(HttpServletRequest request, HttpServletResponse response){
//        String requestURI = request.getRequestURI();
//        String path = request.getContextPath();
        //获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //判断用户名  密码是否为空
        if (!StrUtils.empty(username) ||!StrUtils.empty(password)){
            //跳转到登陆页面
            return  "redirect:/login.jsp";
    }
        //调用业务层
        UserService userService = new UserServiceImpl();
        User user = userService.login(username, password);
        //判断该用户是否为空
        if(user ==null){
            return "redirect:/login.jsp";
        }else {
            //登陆成功 将用户信息存在to'mcat的session中一直携带
            request.getSession().setAttribute("loginUser",user);
            return "redirect:/goods?method=show";
        }

    }

    }

